% Code generates model predictions under various counterfactual preferences
% and beliefs. These underly profiles in Figure 7, Figure A11, Figure A12. 

% The four counterfactuals analysed as are follows: 

% 1) No marriage market return to education
% 2) No preference for education
% 3) No deteriorating marriage market returns to age
% 4) No preference for delaying marriage

% to run each, first run section 0 and then the section in question
%% 0) Load up the main estimates
load('../Output/main_estimates.mat')

pref_spec=2;
n_beliefparms=7;

% import data - ex ante
%-----------------------------
[xadata] = import_and_setup_exante;

% import and setup expost data
%-----------------------------
[xpdata,n_prefparms,n_groomchars] = import_and_setup_expost(pref_spec);

setup_states;
         
% belief coeficients
beliefparms.theta=estimates.beliefs(3:9);
beliefparms.gamma=estimates.beliefs(10);
beliefparms.gamma_mod=estimates.beliefs(2); 
  
fixedparms.ind_goodgirl=1;
pi0_good=makepi(states,fixedparms,beliefparms)

fixedparms.ind_goodgirl=0;
pi0_bad=makepi(states,fixedparms,beliefparms)

pi0=[pi0_good pi0_bad]
[states pi0]

%% counterfactual 1) constant offer probability (i.e. shut down MM returns to ed)

% GO TO SOLVE_PROBLEM AND UNCOMMENT THE PI_OVERRIDE SECTION (BE SURE TO
% COMMENT BACK OUT AFTER RUNNING THIS SECTION)

bparms1=estimates.beliefs;
prefparms1=estimates.prefs;
fixedparms.n_prefparms=n_prefparms
fixedparms.n_beliefparms=n_beliefparms
fixedparms.n_groomchars=n_groomchars
fixedparms.prefparms=prefparms1
fixedparms.disc=0.95;

a=pi0(states(:,1)==16 & states(:,3)==1,:) % use offer probability associated with a 16 year old who is still in school
global pi_counter
pi_counter=pi0
pi_counter(:,1)=a(1)
pi_counter(:,2)=a(2)
pi_counter(:,3)=a(3)
pi_counter(:,4)=a(4)

export_paths('c1_',states,xadata,fixedparms,pref_spec,bparms1)
clear pi_counter
%%  counterfactual 2) shut down prefs for education

bparms1=estimates.beliefs;
prefparms1=estimates.prefs;

prefparms1(11:12)=0
prefparms1(8)=0

fixedparms.n_prefparms=n_prefparms
fixedparms.n_beliefparms=n_beliefparms
fixedparms.n_groomchars=n_groomchars
fixedparms.prefparms=prefparms1
fixedparms.disc=0.95;


export_paths('c2_',states,xadata,fixedparms,pref_spec,bparms1)

%% counterfactual 3) non-deteriorating MM returns.

% GO TO SOLVE_PROBLEM AND UNCOMMENT THE PI_OVERRIDE SECTION (BE SURE TO
% COMMENT BACK OUT AFTER RUNNING THIS SECTION)

bparms1=estimates.beliefs;
prefparms1=estimates.prefs;

fixedparms.n_prefparms=n_prefparms
fixedparms.n_beliefparms=n_beliefparms
fixedparms.n_groomchars=n_groomchars
fixedparms.prefparms=prefparms1
fixedparms.disc=0.95;

% override pi
global pi_counter
pi_counter=pi0

for e=7:12 % get the in-school offer rate even when out of school (with same level of education)
    pi_counter(states(:,2)==e,1)=pi0(states(:,2)==e & states(:,3)==1,1)
    pi_counter(states(:,2)==e,2)=pi0(states(:,2)==e & states(:,3)==1,2)
    pi_counter(states(:,2)==e,3)=pi0(states(:,2)==e & states(:,3)==1,3)
    pi_counter(states(:,2)==e,4)=pi0(states(:,2)==e & states(:,3)==1,4) 
end

    pi_counter(states(:,2)==13,1)=pi0(states(:,2)==13 & states(:,1)==19,1)
    pi_counter(states(:,2)==13,2)=pi0(states(:,2)==13 & states(:,1)==19,2)
    pi_counter(states(:,2)==13,3)=pi0(states(:,2)==13 & states(:,1)==19,3)
    pi_counter(states(:,2)==13,4)=pi0(states(:,2)==13 & states(:,1)==19,4) 

export_paths('c3_',states,xadata,fixedparms,pref_spec,bparms1)

clear pi_counter
%% counterfactual 4) No prefs over age of marriage
bparms1=estimates.beliefs;
prefparms1=estimates.prefs;

prefparms1(9:10)=0

fixedparms.n_prefparms=n_prefparms
fixedparms.n_beliefparms=n_beliefparms
fixedparms.n_groomchars=n_groomchars
fixedparms.prefparms=prefparms1
fixedparms.disc=0.95;

export_paths('c4_',states,xadata,fixedparms,pref_spec,bparms1)
